// pages/store/store.js
let app = getApp()
import { requestApi } from '../../api'
import { formatImage } from '../../utils/formatImage'
import { formatPrice } from '../../utils/util'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    loading: false,
    enableWidth: 0,
    enableHeight: 0,
    className: '',
    registration_city_name: '',
    registration_country_name: '',
    nickname: '',
    followers_count: 0,
    stateNum: '1',
    channel: '',
    following: '',
    attentionIMage: '',
    avatar: '',
    back_img: '',
    topicData: [],  //轮播数据
    enableCouponList: [],
    goodsContainer: [], //商品列表
    indicatorDots: true,
    autoplay: true,
    interval: 3000,
    duration: 1000,
    seller_new_uid: '',
    classReciveName: '',
    announcement: [],
  },
  /**
   * 生命周期函数--监听页面加载
   */
  async onShow() {
    let pages = getCurrentPages();
    let currentPage = pages[pages.length - 1];
    let options = currentPage.options;
    // wx.hideHomeButton();
    let channel = options.channel || 'wx_xcx';
    this.data.channel = channel
    let seller_uid = options.seller_uid;
    if (seller_uid) {
      app.getOpenId().then(async (res) => {
        let openid = res;
        this.getStoreData(seller_uid, channel)
        // 有值更新
        app.addUseBbehavior(openid, seller_uid);
      }).catch(err => {
        
      })

    } else {
      // 没有
      app.getOpenId().then(async (res) => {
        let seller_uid = await app.getSellerId(res)
        this.getStoreData(seller_uid, channel)
      }).catch(err => {
        // console.log(444)
      })
    }



  },

  toGoodsDetail: function (e) {
    let gid = e.currentTarget.id
    wx.navigateTo({
      url: '../goods/goodsDetail/goodsDetail?gid=' + gid + '&channel=' + this.data.channel,
    })
  },


  getStoreData: function (seller_uid, channel) {
    app.request({
      url: requestApi.storeUrl,
      data: {
        seller_uid: seller_uid,
        channel: channel
      },
    }, (data) => {
      let enableCouponList = null
      let classReciveName = null
      let goodsContainer = []

      if (data.original.data.nickname) {
        this.setData({
          nickname: data.original.data.nickname
        })

        wx.setNavigationBarTitle({
          title: data.original.data.nickname
        })
      }

      if (data.original.data.announcement_info) {
        this.setData({
          announcement: data.original.data.announcement_info
        })
      }

      let seller_type = data.original.data.seller_type

      switch (seller_type) {
        case 0:
          this.setData({
            seller_name: '普通用户'
          })
          break;
        case 1:
          this.setData({
            seller_name: '注册买手'
          })
          break;
        case 2:
          this.setData({
            seller_name: '买手店'
          })
          break;
        case 3:
          this.setData({
            seller_name: '精品店'
          })
          break;
        case 4:
          this.setData({
            seller_name: '精品店'
          })
          break;
        case 5:
          this.setData({
            seller_name: '品牌店'
          })
          break;
      }
      this.setData({
        seller_new_uid: data.original.data.seller_id
      })

      if (data.original.data.registration_city_name) {
        this.setData({
          registration_city_name: data.original.data.registration_city_name
        })
      }
      if (data.original.data.registration_country_name) {
        this.setData({
          registration_country_name: data.original.data.registration_country_name
        })
      }
      if (data.original.data.followers_count) {
        this.setData({
          followers_count: data.original.data.followers_count
        })
      }
      if (data.original.data.avatar_image_url) {
        data.original.data.avatar_image_url = formatImage.cdnImage(data.original.data.avatar_image_url, formatImage.THUMBNAIL_L)

        this.setData({
          avatar: data.original.data.avatar_image_url
        })
      }
      if (data.original.data.back_img) {
        this.setData({
          back_img: data.original.data.back_img
        })
      }

      if (data.original.data.seller_bgm && data.original.data.seller_bgm.path) {
        data.original.data.seller_bgm.new_path = formatImage.cdnImage(data.original.data.seller_bgm.path, formatImage.THUMBNAIL_L)

        this.setData({
          seller_bgm: data.original.data.seller_bgm.new_path
        })
      }

      if (data.original.data.qualifications_img && data.original.data.qualifications_img.path) {
        data.original.data.qualifications_img.new_path = formatImage.cdnImage(data.original.data.qualifications_img.path, formatImage.THUMBNAIL_L)
      }
      //轮播数据解析
      if (data.original.data.topics_list && data.original.data.topics_list.length) {
        for (let i = 0, tLen = data.original.data.topics_list.length; i < tLen; i++) {
          if (data.original.data.topics_list[i].banner_image) {
            data.original.data.topics_list[i].banner_image.new_path = formatImage.cdnImage(data.original.data.topics_list[i].banner_image.path, formatImage.THUMBNAIL_L)
          }
        }
        this.setData({
          topicData: data.original.data.topics_list,
        })
      }
      // 优惠卷
      enableCouponList = data.original.data.enable_coupon_list
      if (enableCouponList && enableCouponList.length) {
        let len = enableCouponList.length
        let width = ''

        switch (len) {
          case 1:
            this.setData({
              className: 'couponBg1'
            })
            break;
          case 2:
            this.setData({
              className: 'couponBg2'
            })
            break;
          case 3:
            width = '227rpx';
            this.setData({
              className: 'couponBg3'
            })
            break;
          default:
            this.setData({
              className: 'couponBg4'
            })
            break;
        }
        enableCouponList.forEach((item, index) => {
          item.amount = item.amount ? formatPrice(item.amount / 100) : ''

          if (item.status == 1 || item.status == 0 || item.status == 2) {
            if (len == 1) {
              item.classReciveName = 'couponReceiveOne'
            } else if (len == 2) {
              item.classReciveName = 'couponReceiveTwo'
            } else if (len == 3) {
              item.classReciveName = 'couponReceiveThree'
            } else {
              item.classReciveName = 'couponReceiveFour'
            }
          }
        })

        this.setData({
          enableWidth: width,
          enableCouponList: enableCouponList,
        })
      }
      //商品列表
      if (data.original.data.goods_container && data.original.data.goods_container.length) {
        for (let i = 0, len = data.original.data.goods_container.length; i < len; i++) {

          if (data.original.data.goods_container[i].goods) {
            for (let j = 0, tlen = data.original.data.goods_container[i].goods.length; j < tlen; j++) {
              if (data.original.data.goods_container[i].goods[j].goods_status_icon) {
                data.original.data.goods_container[i].goods[j].goods_status_icon.new_path = formatImage.cdnImage(data.original.data.goods_container[i].goods[j].goods_status_icon.path, formatImage.THUMBNAIL_XS)
              }

              if (data.original.data.goods_container[i].goods[j].product_cover_image) {
                data.original.data.goods_container[i].goods[j].product_cover_image.new_path = formatImage.cdnImage(data.original.data.goods_container[i].goods[j].product_cover_image.path, formatImage.THUMBNAIL_S)
              }

              if (data.original.data.goods_container[i].goods[j].goods_price) {
                if (data.original.data.goods_container[i].goods[j].tax_price) {
                  data.original.data.goods_container[i].goods[j].new_price = formatPrice(data.original.data.goods_container[i].goods[j].tax_price / 100)
                } else {
                  data.original.data.goods_container[i].goods[j].new_price = data.original.data.goods_container[i].goods[j].goods_price ? formatPrice(data.original.data.goods_container[i].goods[j].goods_price / 100) : ''
                }

                data.original.data.goods_container[i].goods[j].new_original_price = data.original.data.goods_container[i].goods[j].original_price ? formatPrice(data.original.data.goods_container[i].goods[j].original_price / 100) : ''

                // if (data.original.data.goods_container[i].goods[j].description) {
                //   delete data.original.data.goods_container[i].goods[j].description
                // }
              }
            }
          }
        }
        //关注
        goodsContainer = data.original.data.goods_container

        this.setData({
          goodsContainer: goodsContainer
        })
      }
      this.setData({
        following: data.original.data.following,
        sellerData: data.original.data,
        loading: true
      })
    })
  },

  //领取优惠券
  receiveCoupon: function (e) {
    let coupon_no = e.currentTarget.dataset.coupon
    let enableCouponList = this.data.enableCouponList
    let len = enableCouponList.length

    enableCouponList.forEach((item) => {
      if (item.coupon_no == coupon_no) {
        if (len == 1) {
          item.classReciveName = 'couponReceiveOne'
        } else if (len == 2) {
          item.classReciveName = 'couponReceiveTwo'
        } else if (len == 3) {
          item.classReciveName = 'couponReceiveThree'
        } else {
          item.classReciveName = 'couponReceiveFour'
        }
      }
    })

    this.setData({
      enableCouponList: enableCouponList
    })

    app.request({
      url: requestApi.receiveStoreCouponUrl,
      data: {
        coupon_no: '' + coupon_no
      },
    }, (data) => {
      wx.showModal({
        title: '提示',
        content: data.original.msg,
        showCancel: false,
      })
    })
  },

  bannerTap: function (e) {
    let tid = e.currentTarget.id

    wx.navigateTo({
      url: '../activity/activityDetail/activityDetail?tId=' + tid,
    })
  },

  toLicense: function () {
    wx.navigateTo({
      url: '../licenseInfo/captcha/captcha?type=1&seller_uid=' + this.data.seller_new_uid,
    })
  },

  //搜搜本店商品
  searchOurGoods: function (e) {
    let keywords = e.detail.value

    wx.navigateTo({
      url: '/pages/categoryHall/categoryHall?keywords=' + keywords + '&channel=' + this.data.channel + '&seller_uid=' + this.data.seller_new_uid,
    })
  },
  //店铺详情
  shopTap: function () {
    wx.navigateTo({
      url: '../storeIntroduce/storeIntroduce?seller_uid=' + this.data.seller_new_uid,
    })
  },

  //添加关注
  changeState: function (e) {
    let following = e.currentTarget.id
    let seller_uid = this.data.seller_new_uid

    if (following == 1) {
      following = 0
    } else {
      following = 1
    }

    app.request({
      url: requestApi.followuserUrl,
      method: 'POST',
      data: {
        seller_uid: '' + seller_uid,
        status: '' + following
      }
    }, (data) => {
      this.setData({
        following: following
      })
    }, error => {
      console.log(error)
    })
  },

  searchAll: function () {
    wx.navigateTo({
      url: '/pages/categoryHall/categoryHall?seller_uid=' + this.data.seller_new_uid + '&seller_name=' + this.data.nickname + '&isStore=1',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let sellerName = this.data.nickname
    let sellerUid = this.data.seller_new_uid
    return {
      title: sellerName,
      path: '/pages/store/store' + '?seller_uid=' + sellerUid,
    }
  }
})